package ua.kiev.tosik.price.modules.clients.gui;

import org.apache.log4j.Logger;
import ua.kiev.tosik.price.gui.MainWindow;
import ua.kiev.tosik.price.models.Clients;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;
import ua.kiev.tosik.price.modules.clients.models.EditorForm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class EditorDialog {

	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.modules.clients.gui");
	private static EditorDialog ourInstance;
	private JDialog dialog;
	private ActionListener closeAction;
	private ModuleConfig config;
	private boolean lockDialog = false;
	private EditorStatusBar statusBar;
	private EditorForm form;

	public static EditorDialog getInstance() {
		if (ourInstance == null) {
			ourInstance = new EditorDialog();
		}
		return ourInstance;
	}

	public void showEditButtons(boolean hide) {
		form.showEditButtons(hide);
	}

	private EditorDialog() {
		logger.debug("First Initialize Editor Dialog GUI");
		config = ModuleConfig.getInstance();
		closeAction = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				close();
			}
		};

		dialog = new JDialog(MainWindow.getInstance().getFrame());
		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		dialog.getRootPane().registerKeyboardAction(closeAction, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
		dialog.setTitle(config.get("clients.editor.title"));
		dialog.setIconImage( new ImageIcon(config.get("clients.editor.icon")).getImage() );
		dialog.setModal(true);
		dialog.setResizable(false);
		dialog.setSize( new Dimension(460, 440) );
		dialog.setLayout( new BorderLayout() );
		dialog.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent ev) {
				close();
			}
		});

		form = new EditorForm();
		statusBar = new EditorStatusBar(this);
		dialog.add(form.getPanel(), BorderLayout.CENTER);
		dialog.add(statusBar.getStatusBar(), BorderLayout.SOUTH);
		dialog.pack();
	}

	public JDialog getDialog() {
		return dialog;
	}

	public void open() {
		dialog.pack();
		dialog.setLocationRelativeTo(MainPanel.getInstance().getPanel());
		dialog.setVisible(true);
	}

	public void close() {
		if ( !lockDialog ) {
			dialog.setVisible(false);
			dialog.dispose();
			logger.debug("Edit Dialog closed");
		}
	}

	public void enableForm(boolean enable) {
		lockDialog = !enable;
		form.enableForm(enable);
	}

	public void setSaveButtonAction(ActionListener recordAction) {
		form.setSaveButtonAction(recordAction);
	}

	public void setFormData(Clients client) {
		form.setFormData(client);
	}

	public Clients getFormData() {
		return form.getFormData();
	}

	public void clearForm() {
		form.clearForm();
	}

	public boolean isLockDialog() {
		return lockDialog;
	}

	public void setLockDialog(boolean lockDialog) {
		this.lockDialog = lockDialog;
	}

	public void setDeleteButtonAction(ActionListener deleteRecordAction) {
		form.setDeleteButtonAction(deleteRecordAction);
	}

	public void setPricesButtonAction(ActionListener pricesRecordAction) {
		form.setPricesButtonAction(pricesRecordAction);
	}
}
